package ThreadDemo1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-15
 * Time: 21:59
 */
public class ThreadDemo28 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //使用 callable 实现从 1 + .... + 1000
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i <= 10; i++) {
                    sum += i;
                }
                return sum;
            }
        };
        //需要交给一个Future来，当作订单
        FutureTask<Integer> task = new FutureTask<>(callable);
        Thread t = new Thread(task);
        t.start();

        System.out.println(task.get());
    }
}
